// import Cesium from "cesium/Cesium";

export class PolylineTrailMaterialProperty {
    constructor(options) {
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._colorSubscription = undefined;
        this.color = options.color;
        this.duration = options.duration;
        this.trailImage = options.trailImage;
        this._time = performance.now();
        this.ratio = options.ratio;

        // var ratio = gl.getUniformLocation(gl.program, 'u_ratio');
        // gl.uniform1f(ratio, 5.0);
    }
}

Object.defineProperties(PolylineTrailMaterialProperty.prototype, {
    isConstant: {
        get: function () {
            return false;
        }
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        }
    },
    color: Cesium.createPropertyDescriptor('color')
});

PolylineTrailMaterialProperty.prototype.getType = function (time) {
    return 'PolylineTrail';
}

PolylineTrailMaterialProperty.prototype.getValue = function (time, result) {
    if (!Cesium.defined(result)) {
        result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
    //result.image = Cesium.Material.PolylineTrailImage;
    result.image = this.trailImage;
    result.time = ((performance.now() - this._time) % this.duration) / this.duration;
    result.ratio = this.ratio;
    return result;
}

PolylineTrailMaterialProperty.prototype.equals = function (other) {
    return this === other || (other instanceof PolylineTrailMaterialProperty && Cesium.Property.equals(this._color, other._color))
}

Cesium.Material.PolylineTrailType = 'PolylineTrail';

Cesium.Material.PolylineTrailImage = require("../assets/images/flow.png");

Cesium.Material.PolylineTrailSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
{ \n\
    czm_material material = czm_getDefaultMaterial(materialInput); \n\
    vec2 st = materialInput.st; \n\
    vec4 colorImage = texture2D(image, vec2(fract((st.s - time)*ratio), st.t)); \n\
    material.alpha = colorImage.a * color.a; \n\
    material.diffuse = color.rgb;\n\
    return material; \n\
} ";
// material.diffuse = (colorImage.rgb + color.rgb) / 2.0;//颜色叠加
// vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));//不重复纹理
// vec4 colorImage = texture2D(image, vec2(fract((st.s - time)*3.0), st.t)); //三重纹理


// 多重纹理
// uniform vec2 repeat;
// vec4 textureValue = texture2D(image, fract(repeat * materialInput.st));


Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailType, {
    fabric: {
        type: Cesium.Material.PolylineTrailType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
            ratio: 1.0,
            image: Cesium.Material.PolylineTrailImage,
            time: 0,
            constantSpeed: 300,
            depthFailMaterial: true
        },
        source: Cesium.Material.PolylineTrailSource
    },
    // fabric: {
    //     type: "Water",
    //     uniforms: {
    //         specularMap: "../assets/images/earthspec1k.jpg",
    //         normalMap: Cesium.buildModuleUrl(
    //             "../assets/images/waterNormals.jpg"
    //         ),
    //         frequency: 10000.0,
    //         animationSpeed: 0.01,
    //         amplitude: 1.0,
    //     },
    // },
    translucent: function (material) {
        return true;
    }
});

Cesium.PolylineTrailMaterialProperty = PolylineTrailMaterialProperty;